package com.lhd.qd.module.system.resource.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhd.qd.common.base.ApiResult;
import com.lhd.qd.common.base.BaseController;
import com.lhd.qd.module.system.resource.model.dto.ResourcePageQuery;
import com.lhd.qd.module.system.resource.model.dto.ResourceSaveDTO;
import com.lhd.qd.module.system.resource.model.vo.MenuTreeVO;
import com.lhd.qd.module.system.resource.model.vo.ResourceListVO;
import com.lhd.qd.module.system.resource.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "资源")
@RequestMapping("resource")
@RestController
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation(value = "分页列表", response = ResourceListVO.class)
    @GetMapping
    public ResponseEntity<ApiResult<IPage<ResourceListVO>>> pageResource(ResourcePageQuery query) {

        return success(resourceService.pageResource(query));
    }

    @ApiOperation(value = "树", response = ResourceListVO.class)
    @GetMapping("/menu/tree")
    public ResponseEntity<ApiResult<List<MenuTreeVO>>> getMenuTree() {

        return success(resourceService.getMenuTree());
    }

    @ApiOperation(value = "详情", response = ResourceListVO.class)
    @GetMapping("{code}")
    public ResponseEntity<ApiResult<ResourceListVO>> getResourceById(@PathVariable("code") String code) {

        return success(resourceService.getResourceById(code));
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public ResponseEntity<ApiResult> saveResource(@Validated @RequestBody ResourceSaveDTO saveDTO) {

        return successOrFail(resourceService.saveResource(saveDTO));
    }

    @ApiOperation(value = "修改")
    @PutMapping("{code}")
    public ResponseEntity<ApiResult> updateResourceById(@PathVariable("code") String code, @Validated @RequestBody ResourceSaveDTO saveDTO) {

        return successOrFail(resourceService.updateResourceById(code, saveDTO));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{code}")
    public ResponseEntity<ApiResult> removeResourceById(@PathVariable("code") String code) {

        return successOrFail(resourceService.removeResourceById(code));
    }
}
